<template>
  <div class="text-upload">
    <h3>文本上传分享</h3>
    <textarea v-model="textContent" placeholder="请输入要分享的文本..." rows="10" class="text-area"
      :disabled="isUploading"></textarea>
    <div class="button-group">
      <button @click="uploadText" class="btn upload-btn" :disabled="isUploading || !textContent.trim()">
        {{ isUploading ? '上传中...' : '上传分享' }}
      </button>
      <button @click="getUploadListClick" class="btn list-btn" :disabled="isUploading">
        获取列表
      </button>
      <button @click="deleteUpload" class="btn delete-btn" :disabled="isUploading || !shareLink">
        删除
      </button>
    </div>

    <!-- 请求结果展示区域 -->
    <div class="response-area">
      <div class="response-header">
        <h4>请求结果</h4>
        <button @click="clearResponse" class="clear-btn">清空</button>
      </div>
      <textarea class="code-block" rows="20">{{ responseText }}</textarea>
    </div>

    <div v-if="shareLink" class="share-link">
      <p>分享链接: <a :href="shareLink" target="_blank">{{ shareLink }}</a></p>
      <button @click="copyLink" class="copy-btn">复制链接</button>
    </div>
    <div v-if="uploadList.length > 0" class="upload-list">
      <h4>已上传列表</h4>
      <ul>
        <li v-for="item in uploadList" :key="item.id">
          <div class="text-item"> {{ item.id }} </div>
           {{ item.content.substring(0, 60) }} 
          <div class="text-item"> {{ item.create_at }}</div>
          <div class="text-item"> {{ item.user_id }}</div>

          <button @click="deleteSpecificUpload(item.id)" class="item-delete-btn">删除</button>
        </li>
      </ul>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useTextUpload } from '../hooks/useUpload.ts'

// 使用文本上传钩子
const {
  textContent,
  shareLink,
  isUploading,
  uploadText,
  copyLink,
  uploadList,
  getUploadList,
  deleteUpload,
  deleteSpecificUpload
} = useTextUpload()

// 新增：用于存储请求结果的响应式变量
const responseText = ref('')

// 重写方法以捕获和显示请求结果
const originalUploadText = uploadText
const originalDeleteUpload = deleteUpload
const originalDeleteSpecificUpload = deleteSpecificUpload

// 重写上传文本方法
const enhancedUploadText = async () => {
  try {
    responseText.value = '正在上传文本...'
    await originalUploadText()
    responseText.value = '上传成功！分享链接: ' + shareLink.value
  } catch (error) {
    responseText.value = '上传失败: ' + (error instanceof Error ? error.message : String(error))
  }
}

// 重写删除方法
const enhancedDeleteUpload = async () => {
  try {
    responseText.value = '正在删除上传内容...'
    await originalDeleteUpload()
    responseText.value = '删除成功！'
  } catch (error) {
    responseText.value = '删除失败: ' + (error instanceof Error ? error.message : String(error))
  }
}

// 重写删除特定项方法
const enhancedDeleteSpecificUpload = async (id: number) => {
  try {
    responseText.value = '正在删除指定上传项...'
    await originalDeleteSpecificUpload(id)
    responseText.value = '删除成功！'
  } catch (error) {
    responseText.value = '删除失败: ' + (error instanceof Error ? error.message : String(error))
  }
}

// 替换原始方法
// 注意：以下代码被注释掉了，这可能导致增强版方法没有被应用
// Object.assign({
//   uploadText: enhancedUploadText,
//   deleteUpload: enhancedDeleteUpload,
//   deleteSpecificUpload: enhancedDeleteSpecificUpload
// })

// 手动替换方法
// uploadText = enhancedUploadText
// deleteUpload = enhancedDeleteUpload
// deleteSpecificUpload = enhancedDeleteSpecificUpload

const getUploadListClick = async () => {
  try {
    console.log('调用获取列表方法')
    responseText.value = '正在获取上传列表...'
    const list = await getUploadList()
    console.log('获取到的列表:', list)

    responseText.value = '获取成功！共 ' + list.length + ' 条记录\n' + JSON.stringify(list, null, 2)
  } catch (error) {
    responseText.value = '获取列表失败: ' + (error instanceof Error ? error.message : String(error))
  }
}

// 新增：清空响应结果的方法
const clearResponse = () => {
  responseText.value = ''
}

// 初始状态
responseText.value = '请点击按钮执行操作以查看结果'
</script>

<style scoped>
.text-upload {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
}

/* 基础按钮样式 - 应用于所有按钮 */
.btn {
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background-color 0.2s;
}

/* 按钮禁用状态 - 统一所有按钮的禁用样式 */
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* 按钮组样式 */
.button-group {
  display: flex;
  gap: 10px;
  margin: 15px 0;
}

/* 上传按钮样式 */
.upload-btn {
  background-color: #2196f3;
  color: white;
}

.upload-btn:hover:not(:disabled) {
  background-color: #0b7dda;
}

/* 列表按钮样式 */
.list-btn {
  background-color: #42b983;
  color: white;
}

.list-btn:hover:not(:disabled) {
  background-color: #3aa876;
}

/* 删除按钮样式 */
.delete-btn {
  background-color: #f44336;
  color: white;
}

.delete-btn:hover:not(:disabled) {
  background-color: #d32f2f;
}

.delete-btn:disabled {
  background-color: #ffcccc;
  cursor: not-allowed;
}

.upload-list {
  margin-top: 20px;
  padding-top: 10px;
  border-top: 1px solid #eee;
}

.upload-list li {
  display: flex;
  justify-content: left;
  align-items: center;
}

.text-item {
  flex: 1;
  padding: 2px 10px;
}


.item-delete-btn {
  background-color: #ff6b6b;
  color: white;
  border: none;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  margin-left: 8px;
}

.text-area {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  resize: vertical;
}

.upload-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.share-link {
  margin-top: 20px;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.copy-btn {
  margin-left: 10px;
  padding: 4px 8px;
  background-color: #2196f3;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

/* 新增：请求结果区域样式 */
.response-area {
  margin-top: 20px;
  padding: 10px;
  background-color: #f8f9fa;
  border-radius: 4px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

/* 响应区域头部样式 */
.response-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

/* 清空按钮样式 */
.clear-btn {
  padding: 4px 8px;
  background-color: #f0f0f0;
  color: #666;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: background-color 0.2s;
}

.clear-btn:hover {
  background-color: #e0e0e0;
}

/* 更新pre标签样式 */
.code-block {
  width: 100%;
  min-height: 120px;
  max-height: 400px;
  padding: 15px;
  background-color: #2d2d2d;
  color: #f8f8f2;
  border-radius: 6px;
  overflow: auto;
  font-family: 'Courier New', Courier, monospace;
  white-space: pre-wrap;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border-left: 3px solid #2196f3;
  font-size: 14px;
}

/* 美化滚动条 */
.code-block::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.code-block::-webkit-scrollbar-track {
  background: #3d3d3d;
  border-radius: 4px;
}

.code-block::-webkit-scrollbar-thumb {
  background: #666;
  border-radius: 4px;
}

.code-block::-webkit-scrollbar-thumb:hover {
  background: #888;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .code-block {
    min-height: 100px;
    max-height: 300px;
    padding: 10px;
    font-size: 12px;
  }
}
</style>